package com.xuebusi.toutiao.admin.api.common.config;

import com.xuebusi.toutiao.admin.api.common.interceptor.AuthInterceptor;
import com.xuebusi.toutiao.admin.api.common.resolver.LoginUserHandlerMethodArgumentResolver;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Resource
    private AuthInterceptor authInterceptor;

    @Resource
    private LoginUserHandlerMethodArgumentResolver methodArgumentResolver;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/mp/v1_0/**")
                /* 文章频道不需要验证token */
                .excludePathPatterns("/mp/v1_0/channels")
                /* 登录和注册不需要验证token */
                .excludePathPatterns("/mp/v1_0/register", "/mp/v1_0/authorizations");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(methodArgumentResolver);
    }
}
